using Lockstep.Collision2D;
using Lockstep.Game;
using Lockstep.Math;
using UnityEngine;
using UnityEngine.UI;

namespace Lockstep.Game {
    public class BulletEntityView : BaseEntityView {
        //public UIFloatBar uiFloatBar;
        public BulletEntity entity;

        public override void BindEntity(BaseEntity e, BaseEntity oldEntity = null)
        {
            base.BindEntity(e, oldEntity);
            e.EntityView = this;
            this.entity = e as BulletEntity;
        }

        public override void OnDead()
        {
            GameObject.Destroy(gameObject);
        }

        public override void OnRollbackDestroy()
        {
            GameObject.Destroy(gameObject);
        }

        private void Update()
        {
            var pos = entity.transform.pos.ToVector3();
            transform.position = Vector3.Lerp(transform.position, pos, LerpPercent);
            var deg = entity.transform.deg.ToFloat();
            //deg = Mathf.Lerp(transform.rotation.eulerAngles.y, deg, LerpPercent);
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 180, deg), LerpPercent);
        }
        public void OnDrawGizmos()
        {
#if UNITY_EDITOR
            var col = entity.config.colliderData;
            if (col.type == Collision2D.ColliderType.Circle)
            {
                //circle
                var pos = entity.transform.TransformPoint(col.pos);
                Debug.Log(pos);
                Gizmos.DrawSphere(pos.ToVector3(), col.radius.ToFloat());
            }
            else if (col.type == Collision2D.ColliderType.OBB)
            {
                //aabb
                var pos = entity.transform.TransformPoint(col.pos);
                Gizmos.DrawCube(pos.ToVector3XZ(LFloat.one), col.size.ToVector3XZ(LFloat.one));
                DebugExtension.DebugLocalCube(Matrix4x4.TRS(
                        pos.ToVector3XZ(LFloat.one),
                        Quaternion.Euler(0, 0, entity.transform.deg.ToFloat()),
                        Vector3.one),
                    col.size.ToVector3XZ(LFloat.one), Gizmos.color);
            }
#endif
        }
    }
}